package com.fengwo.andrew;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Main {
    private static final String APP_SECRET_KEY = "Uc4P5sgfoVFc1dbHuhx8+Q==";
    private static final String MY_PACKAGE_NAME = "com.laijian.customer";

    private static final Logger logger = LoggerFactory.getLogger("Main");

    private void sendMessage() throws Exception {
        Constants.useOfficial();
        Sender sender = new Sender(APP_SECRET_KEY);
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        String formattedDate = sdf.format(new Date());
        String messagePayload = "This is a message";
        String title = "notification title" + formattedDate;
        String description = "notification description";

        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(MY_PACKAGE_NAME)
                .notifyType(1) // 使用默认提示音提示
                .extra("channel_id", "131416") // 131415 131416 131417 131418
                .build();
        // Message message = new Message.Builder()
        // .title(title)
        // .description(description).payload(messagePayload)
        // .restrictedPackageName(MY_PACKAGE_NAME)
        // .notifyType(1) // 使用默认提示音提示
        // .extra(Constants.EXTRA_PARAM_SOUND_URI, "android.resource://" +
        // MY_PACKAGE_NAME + "/raw/neworder")
        // .build();

        String regId = "bYH9sv/9u1Tq82jDSPuOo33bP+Yk8sApq08pSR5N6zg85yfoFrN20OMVjTeqvDqe";
        // 2025-01-04 10:00:18.217 16554-5569 JIGUANG-JPush com.laijian.customer I
        // [ThirdPushManager] uploadRegID
        // regid:fzEmmqxsHUhs3pYUJ3z+NToNIKCYoZjcJN7ffCX/ozFXHIt7E46CmCtYLVqN8syb
        Result result = sender.send(message, regId, 3);
        System.out.println("Server response: " + "MessageId: " + result.getMessageId()
                + " ErrorCode: " + result.getErrorCode().toString()
                + " Reason: " + result.getReason());
        System.out.println("推送" + result.getErrorCode().getDescription());
        // logger.error("Server response: ", "MessageId: " + result.getMessageId()
        // + " ErrorCode: " + result.getErrorCode().toString()
        // + " Reason: " + result.getReason());
    }

    public static void main(String[] args) {
        // System.out.println("小米推送");
        Main mi = new Main();
        try {
            mi.sendMessage();
        } catch (Exception e) {
            logger.error("Server response: ", e);
        }

        // mi.sendReqeustForGetMetho();

    }

    private void sendReqeustForGetMetho() {
        // 创建OkHttpClient实例
        OkHttpClient client = new OkHttpClient();

        // 构建请求对象
        Request request = new Request.Builder()
                .url("https://www.baidu.com") // 替换为实际的URL
                .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 获取响应体并打印
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMessageToUserAccount() throws Exception {
        Constants.useOfficial();
        Sender sender = new Sender(APP_SECRET_KEY);
        String messagePayload = "This is a message";
        String title = "notification title";
        String description = "notification description";
        String useraccount = "15112565913"; // useraccount非空白, 不能包含逗号, 长度小于128
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(MY_PACKAGE_NAME)
                .notifyType(1) // 使用默认提示音提示
                .build();

        Result result = sender.sendToUserAccount(message, useraccount, 3); // 根据useraccount, 发送消息到指定设备上

        logger.error("Server response: ", "MessageId: " + result.getMessageId()
                + " ErrorCode: " + result.getErrorCode().toString()
                + " Reason: " + result.getReason());
    }
}